package com.md.notes;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.md.notes.fragment.NoteFragment;
import com.md.notes.fragment.SetFragment;

public class MainActivity extends AppCompatActivity {
    private AppCompatActivity activity;

    private RadioGroup bottomNavigation;
    private RadioButton radioHome;
    private RadioButton radioList;

    private Fragment noteFragment, setFragment;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initView();
        initData();
        addListener();
    }

    private void initView() {
        activity = MainActivity.this;
        bottomNavigation = (RadioGroup)findViewById(R.id.bottom_navigation);
        radioHome = (RadioButton) findViewById(R.id.radio_home);
        radioList = (RadioButton) findViewById(R.id.radio_search);
    }

    private void initData() {
        // 初始化FragmentManager
        fragmentManager = getSupportFragmentManager();
        noteFragment = NoteFragment.newInstance("A","B");
        setFragment = SetFragment.newInstance("C","D");

        switchFragment(noteFragment);
    }

    private void addListener() {
// 设置RadioGroup的选择监听器
        bottomNavigation.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId==R.id.radio_home){
                    switchFragment(noteFragment);
                }
                if(checkedId==R.id.radio_search){
                    switchFragment(setFragment);
                }
            }
        });
    }


    private void switchFragment(Fragment fragment) {
        // 使用FragmentManager进行Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_container, fragment); // 假设有一个ID为fragment_container的容器
        transaction.commit();
    }

}